<?php
class Utils {
	
	public static function loginUser(array $userData) {
	
		session_start();
		$userPrincipal = new UserPrincipal($userData);
		$_SESSION['user'] = $userPrincipal;

		header('Location: office.php');
	}
	
	public static function logoutUser() {
	
		session_destroy();
		unset($_SESSION['user']);
	
		header('Location: index.php');
	}
	
	public static function shuffle_assoc(&$array) {
		$keys = array_keys($array);
	
		shuffle($keys);
	
		foreach($keys as $key) {
			$new[$key] = $array[$key];
		}
	
		$array = $new;
	
		return true;
	}
	
	public static function recharge(Profile $profile, $request) {
		
		
		$user = $profile->getUser();
		
		if($request['beers'] == 1) {
			
			$user->setLockedPeriod(45);
			$currentDate = new DateTime();
			$user->setLockedTime($currentDate);
			$currentEnergy = $user->getEnergy();
			$user->setEnergy($currentEnergy + 2);
			echo "<p>Вече си в бирарията! В следващите 45 минути няма да можеш да се заяждаш с никого!</p>";
			
		} else if($request['beers'] == 2) {
				
			$user->setLockedPeriod(80);
			$currentDate = new DateTime();
			$user->setLockedTime($currentDate);
			$currentEnergy = $user->getEnergy();
			$user->setEnergy($currentEnergy + 4);
			echo "<p>Вече си в бирарията! В следващите 80 минути няма да можеш да се заяждаш с никого!</p>";
			
		} else if($request['beers'] == 3) {
		
			$user->setLockedPeriod(120);
			$currentDate = new DateTime();
			$user->setLockedTime($currentDate);
			$currentEnergy = $user->getEnergy();
			$user->setEnergy($currentEnergy + 6);
			echo "<p>Вече си в бирарията! В следващите 120 минути няма да можеш да се заяждаш с никого!</p>";
			
		} 
	}
	
	public static function buyUnit(User $user, $unitID, $number) {
	
		if(get_class($user) != "User") {
				
			throw new Exception("First parameter for function buyUnit should be of type User");
		}
	
		if($unitID<0) {
				
			throw new Exception("Second parameter for function buyUnit should be a positive integer number!");
		}
	
		if($number<0) {
	
			throw new Exception("Third parameter for function buyUnit should be a positive integer number!");
		}
	
		$units = DBUtils::getUnits();
		$price = null;
		$attack = null;
		$defence = null;
		$name = null;
		foreach($units as $unit) {
				
			if($unit['unit_id'] == $unitID) {
	
				$price = $unit['price'];
				$attack = $unit['attack'];
				$defence = $unit['deffence'];
	
			}
		}
	
		if($price == null) {
				
			throw new Exception("Parameter for Unit ID is invalid. There is no such unit in a market!");
		}
	
		$check = $number * $price;
		$userMoney = $user->getMoney();
	
		if($check > $userMoney) {
				
			echo "Нямате достатъчно пари, за да наемете толкова програмисти!";
				
		} else {
				
			$user->setAttack($user->getAttack() + ($attack*$number));
			$user->setDefence($user->getDefence() + ($defence*$number));
			$user->setMoney($userMoney-$check);
				
			if($number == 1) {
	
				echo "Честито, успешно наехте $number програмист във вашия екип.";
	
			} else {
	
			echo "Честито, успешно наехте $number програмисти във вашия екип.";
			}
				
			}
		}
}